<?php
namespace app\shop\controller;
use app\common\controller\Common;
use app\shop\model\OrderModel;
use app\shop\model\AfterModel;
use app\common\model\Category;
use app\common\model\OrderGoods;
use app\common\model\Supplier;
use app\common\model\Order as OrderIn;
use think\Exception;
use think\Request;

/**
* 订单管理
*/
class Order extends Common
{
	/**
    * 订单管理
    */
    public function orderlist()
    {
        $info = [];
        if(request()->isPost())
		{
			$info = Request::instance()->param();
		}
        list($order,$xsdd,$xsze,$dfk) = OrderModel::getOrderList($info);
        $this->assign('order',$order);
        $this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
        /*销售订单数*/
        $this->assign('xsdd',$xsdd);
        /*销售总额*/
        $this->assign('xsze',$xsze);
        /*待付款总额*/
        $this->assign('dfk',$dfk);
        return $this->fetch();
    }
    /**
    * 订单详情
    */
    public function orderinfo()
    {
        $info = Request::instance()->param();
        list($order,$OrderGoods,$OrderActivity) = OrderModel::orderInfo($info);
        
        $this->assign('order',$order);
        $this->assign('ordergoods',$OrderGoods);
        $this->assign('orderactivity',$OrderActivity);
        return $this->fetch();
    }
    /**
    * 每日营业额
    */
    public function turnover()
    {
        $info = [];
        if(request()->isPost())
		{
			$info = Request::instance()->param();
		}
        list($order,$xsdd,$xjzf,$sjzf,$yhzf,$xsze,$dfk) = OrderModel::turnover($info);
        $this->assign('order',$order);
        $this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
        /*销售订单数*/
        $this->assign('xsdd',$xsdd);
        /*现金支付*/
        $this->assign('xjzf',$xjzf);
        /*手机支付*/
        $this->assign('sjzf',$sjzf);
        /*银行卡支付*/
        $this->assign('yhzf',$yhzf);
        /*销售总额*/
        $this->assign('xsze',$xsze);
        /*待付款总额*/
        $this->assign('dfk',$dfk);
        return $this->fetch();
    }
    /**
    * 订单详情
    */
    public function turnoverinfo()
    {
        $info = Request::instance()->param();
        list($order,$OrderGoods,$OrderActivity) = OrderModel::orderInfo($info);
        
        $this->assign('order',$order);
        $this->assign('ordergoods',$OrderGoods);
        $this->assign('orderactivity',$OrderActivity);
        return $this->fetch();
    }
    /**
    * 退货、召回
    */
    public function orderafter()
    {
        $info = Request::instance()->param();
        if(request()->isPost())
		{
			try{
				return json(OrderModel::addAfterOrder($info));
            }catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
        $this->assign('datetime',time());
        /*获取订单商品数据*/
        $where['order_goods_id'] = $info['id'];
        $orderGoodsInfo = (new OrderGoods())->orderGoodsInfo($where,'*');
        $this->assign('goods',$orderGoodsInfo);
        /*获取订单信息*/
        $where1['order_id'] = $orderGoodsInfo['order_id'];
        $orderInfo = (new OrderIn())->orderInfo($where1);
        $this->assign('order',$orderInfo);
        $this->assign('supp',(new Supplier())->supplierList(['is_show'=>0],'supplier_id,supplier_name'));
        return $this->fetch();
    }
    /**
    * 退货、召回管理
    */
    public function orderafterlist()
    {
        $info = Request::instance()->param();
        list($order,$thdd,$thze) = AfterModel::getAfterList($info);
        $this->assign('order',$order);
        $this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
        /*退货订单数*/
        $this->assign('thdd',$thdd);
        /*退货总金额*/
        $this->assign('thze',$thze);
        /*类型*/
        $this->assign('ty',$info['ty']);
        $this->assign('supp',(new Supplier())->supplierList(['is_show'=>0],'supplier_id,supplier_name'));
        return $this->fetch();
    }
    /**
    * 增加付款金额
    */
    public function addmoney()
    {
        $info = Request::instance()->param();
        if(request()->isPost())
		{
			try{
				return json(OrderModel::addMoney($info));
            }catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
        $this->assign('id',$info['id']);
        return $this->fetch();
    }
    /**
    * 付款记录
    */
    public function moneylog()
    {
        $info = Request::instance()->param();
        list($payLogList,$totl) = OrderModel::moneyLog($info);
        
        $this->assign('payLogList',$payLogList);
        $this->assign('totl',$totl);
        return $this->fetch();
    }
    /**
    * 退货、召回详情信息
    */
    public function afterinfo()
    {
        $info = Request::instance()->param();
        list($after,$order,$ordergoods,$orderActivity) = AfterModel::afterInfo($info);
        
        $this->assign('after',$after);
        $this->assign('order',$order);
        $this->assign('ordergoods',$ordergoods);
        $this->assign('orderactivity',$orderActivity);
        return $this->fetch();
    }
    /**
    * 交易管理
    */
    public function order()
    {
        if(request()->isPost())
		{
			$info = Request::instance()->param();
			return OrderModel::getOrderGoods($info);
		}
        return $this->fetch();
    }
}
